package display;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import arithmetique.Ensemble;
import arithmetique.Financing;

/**
 * Affiche la liste des sources de financement
 */
public class Liste {

	public Ensemble ensemble;
	public List listeSources;
	private Resultat resultat;
	
	public Liste(Shell sShell) {
		ensemble = new Ensemble();
		
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.verticalSpacing  =0;
		gridLayout.marginHeight =0;
		gridLayout.marginWidth =3;
		gridLayout.marginTop =3;
		Group groupSources = new Group(sShell, SWT.NONE);
		groupSources.setLayout(gridLayout);
		groupSources.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		groupSources.setText("sources de financement");

		GridData listeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		listeGridData.horizontalSpan = 2;
		listeSources = new List(groupSources, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE);
		listeSources.setLayoutData(listeGridData);

		final Button boutonDelete = new Button(groupSources, SWT.PUSH);
		boutonDelete.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		boutonDelete.setText("Supprimer");
		boutonDelete.setEnabled(false);
		Button boutonDeleteAll = new Button(groupSources, SWT.PUSH);
		boutonDeleteAll.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		boutonDeleteAll.setText("Supprimer tout");
		final Cursor cursor = new Cursor(sShell.getDisplay(), SWT.CURSOR_HAND);
		boutonDelete.setCursor(cursor);
		boutonDeleteAll.setCursor(cursor);

		Listener deleteEnableListener = new Listener() {
			public void handleEvent(Event event) {
				if ( listeSources.getSelectionCount()>0 ){
					boutonDelete.setEnabled(true);
				}else {
					boutonDelete.setEnabled(false);
				}
			}
		};

		listeSources.addListener(SWT.Selection, deleteEnableListener);

		boutonDelete.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) { }
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				ensemble.deleteFinancing(listeSources.getSelectionIndex());
				listeSources.remove(listeSources.getSelectionIndex());
				resultat.redraw(0,true);
				boutonDelete.setEnabled(false);
			}
		});
		boutonDeleteAll.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) { }
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				ensemble.deleteAll();
				listeSources.removeAll();
				resultat.redraw(0,true);
				boutonDelete.setEnabled(false);
			}
		});
		
	}

	public void add(Financing dernierAjout) {
		ensemble.addFinancing(dernierAjout);
		listeSources.add(dernierAjout.toString());
		resultat.redraw(0,true);
	}

	public void dispose() {
	}

	public Ensemble getEnsemble() {
		return ensemble;
	}

	public void setResultat(Resultat resultat) {
		this.resultat = resultat;
	}
}
